<template>
  <div class="login-container">
    <!-- 左侧插画区域 -->
    <div class="login-left">
      <div class="illustration-wrapper">
        <img src="../../../assets/images/login_illustration.png" alt="登录插画" class="illustration-img">
      </div>
    </div>
    
    <!-- 右侧登录表单区域 -->
    <div class="login-right">
      <div class="login-form-wrapper">
        <div class="login-form-container">
          <h2 class="login-title">登录</h2>
          
          <el-form ref="loginForm" :model="formLogin" :rules="rules" class="login-form">
            <!-- 隐藏系统域 -->
            <el-form-item prop="domain" style="display: none;">
              <el-input type="hidden" v-model="formLogin.domain"></el-input>
            </el-form-item>
            
            <!-- 用户名输入框 -->
            <el-form-item prop="username">
              <el-input 
                v-model="formLogin.username" 
                type="text" 
                placeholder="EMSUser"
                class="login-input"
                prefix-icon="el-icon-user">
              </el-input>
            </el-form-item>
            
            <!-- 密码输入框 -->
            <el-form-item prop="password">
              <el-input 
                v-model="formLogin.password" 
                type="password" 
                placeholder="••••••"
                class="login-input"
                prefix-icon="el-icon-lock"
                @keyup.enter.native="submit">
              </el-input>
            </el-form-item>
            
            <!-- 验证码输入框 -->
            <el-form-item class="captcha-row">
              <el-input 
                v-model="captchaCode" 
                placeholder="验证码"
                class="captcha-input">
              </el-input>
              <div class="captcha-image">
                <canvas ref="captchaCanvas" width="100" height="40" @click="generateCaptcha"></canvas>
              </div>
            </el-form-item>
            
            <!-- 记住密码 -->
            <el-form-item class="remember-row">
              <el-checkbox v-model="rememberMe" class="remember-checkbox">记住密码</el-checkbox>
            </el-form-item>
            
            <!-- 登录按钮 -->
            <el-form-item>
              <el-button 
                :loading="loading" 
                type="primary" 
                class="login-button"
                @click="submit">
                <span v-if="!loading">登录</span>
                <span v-else>登录中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { v4 as uuidv4 } from 'uuid'

export default {
  name: "Login",
  data() {
    return {
      loading: false,
      rememberMe: false,
      captchaCode: '',
      formLogin: {
        domain: '12345678',
        username: '',
        password: '',
      },
      rules: {
        username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
        password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
      },
    };
  },
  mounted() {
    this.generateCaptcha();
  },
  methods: {
    ...mapActions('d2admin/account', ['login']),
    
    // 生成验证码
    generateCaptcha() {
      const canvas = this.$refs.captchaCanvas;
      const ctx = canvas.getContext('2d');
      
      // 清空画布
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      
      // 设置背景
      const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
      gradient.addColorStop(0, '#f0f2f5');
      gradient.addColorStop(1, '#e8eaed');
      ctx.fillStyle = gradient;
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      
      // 生成随机验证码
      const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      let captcha = '';
      for (let i = 0; i < 4; i++) {
        captcha += chars.charAt(Math.floor(Math.random() * chars.length));
      }
      
      // 绘制验证码文字
      ctx.font = '20px Arial';
      ctx.fillStyle = '#666';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      
      for (let i = 0; i < captcha.length; i++) {
        const x = 20 + i * 20;
        const y = 20 + Math.random() * 5 - 2.5;
        const angle = (Math.random() - 0.5) * 0.3;
        
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle);
        ctx.fillText(captcha[i], 0, 0);
        ctx.restore();
      }
      
      // 添加干扰线
      for (let i = 0; i < 3; i++) {
        ctx.strokeStyle = `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.3)`;
        ctx.beginPath();
        ctx.moveTo(Math.random() * canvas.width, Math.random() * canvas.height);
        ctx.lineTo(Math.random() * canvas.width, Math.random() * canvas.height);
        ctx.stroke();
      }
      
      this.currentCaptcha = captcha;
    },
    
    submit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.login({
            domain: this.formLogin.domain,
            username: this.formLogin.username,
            password: this.formLogin.password,
            captcha: '8888',
            t: Date.now(),
            uuid: uuidv4(),
          }).then(() => {
            this.loading = false;
            this.$router.replace(this.$route.query.redirect || '/')
          }).catch(() => {
            this.loading = false;
            this.generateCaptcha();
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  height: 100vh;
  background: linear-gradient(135deg, #1e3a5f 0%, #2d4a6b 50%, #3a5a7a 100%);
  position: relative;
  overflow: hidden;
  
  // 添加背景点阵图案
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
    background-size: 20px 20px;
    opacity: 0.3;
    z-index: 1;
  }
}

.login-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  position: relative;
  z-index: 2;
  
  .illustration-wrapper {
    max-width: 600px;
    width: 100%;
    
    .illustration-img {
      width: 100%;
      height: auto;
      filter: drop-shadow(0 10px 30px rgba(0, 0, 0, 0.3));
    }
  }
}

.login-right {
  width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  z-index: 2;
  
  .login-form-wrapper {
    width: 100%;
    max-width: 400px;
  }
  
  .login-form-container {
    background: #ffffff;
    border-radius: 16px;
    padding: 40px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #1a73e8;
  text-align: center;
  margin: 0 0 40px 0;
  letter-spacing: 1px;
}

.login-form {
  .el-form-item {
    margin-bottom: 24px;
    
    &.captcha-row {
      .el-form-item__content {
        display: flex;
        gap: 12px;
      }
    }
    
    &.remember-row {
      margin-bottom: 32px;
    }
  }
}

.login-input {
  .el-input__inner {
    height: 48px;
    border: 2px solid #e8eaed;
    border-radius: 8px;
    font-size: 16px;
    padding-left: 45px;
    transition: all 0.3s ease;
    
    &:focus {
      border-color: #1a73e8;
      box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
    }
    
    &::placeholder {
      color: #9aa0a6;
      font-size: 14px;
    }
  }
  
  .el-input__prefix {
    left: 15px;
    color: #9aa0a6;
    font-size: 18px;
  }
}

.captcha-input {
  flex: 1;
  
  .el-input__inner {
    height: 48px;
    border: 2px solid #e8eaed;
    border-radius: 8px;
    font-size: 16px;
    transition: all 0.3s ease;
    
    &:focus {
      border-color: #1a73e8;
      box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
    }
  }
}

.captcha-image {
  width: 100px;
  height: 48px;
  border: 2px solid #e8eaed;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8f9fa;
  transition: all 0.3s ease;
  
  &:hover {
    border-color: #1a73e8;
  }
  
  canvas {
    border-radius: 6px;
  }
}

.remember-checkbox {
  .el-checkbox__label {
    color: #5f6368;
    font-size: 14px;
  }
  
  .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #1a73e8;
    border-color: #1a73e8;
  }
}

.login-button {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #1a73e8 0%, #4285f4 100%);
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  transition: all 0.3s ease;
  
  &:hover {
    background: linear-gradient(135deg, #1557b0 0%, #3367d6 100%);
    transform: translateY(-1px);
    box-shadow: 0 8px 25px rgba(26, 115, 232, 0.3);
  }
  
  &:active {
    transform: translateY(0);
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: none;
    height: 40%;
    padding: 30px;
    
    .illustration-wrapper {
      max-width: 400px;
    }
  }
  
  .login-right {
    width: 100%;
    height: 60%;
    padding: 20px;
  }
}

@media (max-width: 768px) {
  .login-left {
    height: 30%;
    padding: 20px;
  }
  
  .login-right {
    height: 70%;
    
    .login-form-container {
      padding: 30px 20px;
    }
  }
  
  .login-title {
    font-size: 24px;
    margin-bottom: 30px;
  }
}
</style>

